using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using  C = GM.Constants;

namespace GM
{
	/// <summary>
	/// Summary description for ResizeMethod.
	/// </summary>
	public class ResizeMethodControl : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.RadioButton rbMethodKip;
		private System.Windows.Forms.RadioButton rbMethodFit;
		private System.Windows.Forms.RadioButton rbMethodFitHeight;
		private System.Windows.Forms.RadioButton rbMethodFitWidth;
		private System.Windows.Forms.Button btnDefault;
		private System.Windows.Forms.GroupBox gbAlign;
		private System.Windows.Forms.GroupBox gbResizeMethod;
		private System.Windows.Forms.GroupBox gbMethod;

		private System.Windows.Forms.Button btnAlignLD;
		private System.Windows.Forms.Button btnAlignLC;
		private System.Windows.Forms.Button btnAlignRD;
		private System.Windows.Forms.Button btnAlignLU;
		private System.Windows.Forms.Button btnAlignU;
		private System.Windows.Forms.Button btnAlignD;
		private System.Windows.Forms.Button btnAlignRU;
		private System.Windows.Forms.Button btnAlignRC;
		private System.Windows.Forms.Button btnAlignC;

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private ArrayList alignButtons = new ArrayList();
		private Color defaultColor;

		#region Properties
		public string Method {
			get {
				if (rbMethodKip.Checked) return C.METHOD_KIP;
				else if (rbMethodFit.Checked) return C.METHOD_FIT;
				else if (rbMethodFitHeight.Checked) return C.METHOD_FIT_HEIGHT;
				else if (rbMethodFitWidth.Checked) return C.METHOD_FIT_WIDTH;
				else return C.METHOD_KIP;
			}
			set {
				switch (value) {
					case C.METHOD_KIP:
						rbMethodKip.Checked = true;
						break;
					case C.METHOD_FIT:
						rbMethodFit.Checked = true;
						break;
					case C.METHOD_FIT_HEIGHT:
						rbMethodFitHeight.Checked = true;
						break;
					case C.METHOD_FIT_WIDTH:
						rbMethodFitWidth.Checked = true;
						break;
					default:
						rbMethodKip.Checked = true;
						SetAlignButtonsDefaultColor();
						align = C.ALIGN_CENTER;
						btnAlignC.BackColor = Color.Red;
						break;
				}
			}
		}


		private string align = "AC";
		public string Align {
			get { return align; }
			set {
				SetAlignButtonsDefaultColor();
				align = value; 
				switch (value) {
					case C.ALIGN_CENTER:
						btnAlignC.BackColor = Color.Red;
						break;
					case C.ALIGN_CENTER_LEFT:
						btnAlignLC.BackColor = Color.Red;
						break;
					case C.ALIGN_CENTER_RIGHT:
						btnAlignRC.BackColor = Color.Red;
						break;
					case C.ALIGN_UP:
						btnAlignU.BackColor = Color.Red;
						break;
					case C.ALIGN_UP_LEFT:
						btnAlignLU.BackColor = Color.Red;
						break;
					case C.ALIGN_UP_RIGHT:
						btnAlignRU.BackColor = Color.Red;
						break;
					case C.ALIGN_DOWN:
						btnAlignD.BackColor = Color.Red;
						break;
					case C.ALIGN_DOWN_LEFT:
						btnAlignLD.BackColor = Color.Red;
						break;
					case C.ALIGN_DOWN_RIGHT:
						btnAlignRD.BackColor = Color.Red;
						break;
					default:
						align = C.ALIGN_CENTER;
						btnAlignC.BackColor = Color.Red;
						break;
				}
			}
		}
		#endregion

		public ResizeMethodControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			alignButtons.Add(btnAlignD);
			alignButtons.Add(btnAlignLC);
			alignButtons.Add(btnAlignC);
			alignButtons.Add(btnAlignLD);
			alignButtons.Add(btnAlignLU);
			alignButtons.Add(btnAlignRC);
			alignButtons.Add(btnAlignRD);
			alignButtons.Add(btnAlignRU);
			alignButtons.Add(btnAlignU);
			defaultColor = btnDefault.BackColor;
			btnAlignC.BackColor = Color.Red;
			SetAlignEnabled(false);
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.gbResizeMethod = new System.Windows.Forms.GroupBox();
			this.gbAlign = new System.Windows.Forms.GroupBox();
			this.btnAlignLD = new System.Windows.Forms.Button();
			this.btnAlignLC = new System.Windows.Forms.Button();
			this.btnAlignRD = new System.Windows.Forms.Button();
			this.btnAlignLU = new System.Windows.Forms.Button();
			this.btnAlignC = new System.Windows.Forms.Button();
			this.btnAlignU = new System.Windows.Forms.Button();
			this.btnAlignD = new System.Windows.Forms.Button();
			this.btnAlignRU = new System.Windows.Forms.Button();
			this.btnAlignRC = new System.Windows.Forms.Button();
			this.btnDefault = new System.Windows.Forms.Button();
			this.gbMethod = new System.Windows.Forms.GroupBox();
			this.rbMethodFitWidth = new System.Windows.Forms.RadioButton();
			this.rbMethodFitHeight = new System.Windows.Forms.RadioButton();
			this.rbMethodFit = new System.Windows.Forms.RadioButton();
			this.rbMethodKip = new System.Windows.Forms.RadioButton();
			this.gbResizeMethod.SuspendLayout();
			this.gbAlign.SuspendLayout();
			this.gbMethod.SuspendLayout();
			this.SuspendLayout();
			// 
			// gbResizeMethod
			// 
			this.gbResizeMethod.Controls.Add(this.gbAlign);
			this.gbResizeMethod.Controls.Add(this.gbMethod);
			this.gbResizeMethod.Controls.Add(this.btnDefault);
			this.gbResizeMethod.Location = new System.Drawing.Point(8, 8);
			this.gbResizeMethod.Name = "gbResizeMethod";
			this.gbResizeMethod.Size = new System.Drawing.Size(328, 144);
			this.gbResizeMethod.TabIndex = 0;
			this.gbResizeMethod.TabStop = false;
			this.gbResizeMethod.Text = "Resize Method";
			// 
			// gbAlign
			// 
			this.gbAlign.Controls.Add(this.btnAlignLD);
			this.gbAlign.Controls.Add(this.btnAlignLC);
			this.gbAlign.Controls.Add(this.btnAlignRD);
			this.gbAlign.Controls.Add(this.btnAlignLU);
			this.gbAlign.Controls.Add(this.btnAlignC);
			this.gbAlign.Controls.Add(this.btnAlignU);
			this.gbAlign.Controls.Add(this.btnAlignD);
			this.gbAlign.Controls.Add(this.btnAlignRU);
			this.gbAlign.Controls.Add(this.btnAlignRC);
			this.gbAlign.Location = new System.Drawing.Point(120, 16);
			this.gbAlign.Name = "gbAlign";
			this.gbAlign.Size = new System.Drawing.Size(112, 120);
			this.gbAlign.TabIndex = 1;
			this.gbAlign.TabStop = false;
			this.gbAlign.Text = "Align";
			// 
			// btnAlignLD
			// 
			this.btnAlignLD.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignLD.Location = new System.Drawing.Point(8, 80);
			this.btnAlignLD.Name = "btnAlignLD";
			this.btnAlignLD.Size = new System.Drawing.Size(32, 32);
			this.btnAlignLD.TabIndex = 5;
			this.btnAlignLD.Text = "+";
			this.btnAlignLD.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			this.btnAlignLD.Click += new System.EventHandler(this.btnAlignLD_Click);
			// 
			// btnAlignLC
			// 
			this.btnAlignLC.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignLC.Location = new System.Drawing.Point(8, 48);
			this.btnAlignLC.Name = "btnAlignLC";
			this.btnAlignLC.Size = new System.Drawing.Size(32, 32);
			this.btnAlignLC.TabIndex = 4;
			this.btnAlignLC.Text = "+";
			this.btnAlignLC.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnAlignLC.Click += new System.EventHandler(this.btnAlignLC_Click);
			// 
			// btnAlignRD
			// 
			this.btnAlignRD.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignRD.Location = new System.Drawing.Point(72, 80);
			this.btnAlignRD.Name = "btnAlignRD";
			this.btnAlignRD.Size = new System.Drawing.Size(32, 32);
			this.btnAlignRD.TabIndex = 11;
			this.btnAlignRD.Text = "+";
			this.btnAlignRD.TextAlign = System.Drawing.ContentAlignment.BottomRight;
			this.btnAlignRD.Click += new System.EventHandler(this.btnAlignRD_Click);
			// 
			// btnAlignLU
			// 
			this.btnAlignLU.AllowDrop = true;
			this.btnAlignLU.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignLU.Location = new System.Drawing.Point(8, 16);
			this.btnAlignLU.Name = "btnAlignLU";
			this.btnAlignLU.Size = new System.Drawing.Size(32, 32);
			this.btnAlignLU.TabIndex = 3;
			this.btnAlignLU.Text = "+";
			this.btnAlignLU.TextAlign = System.Drawing.ContentAlignment.TopLeft;
			this.btnAlignLU.Click += new System.EventHandler(this.btnAlignLU_Click);
			// 
			// btnAlignC
			// 
			this.btnAlignC.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignC.Location = new System.Drawing.Point(40, 48);
			this.btnAlignC.Name = "btnAlignC";
			this.btnAlignC.Size = new System.Drawing.Size(32, 32);
			this.btnAlignC.TabIndex = 7;
			this.btnAlignC.Text = "+";
			this.btnAlignC.Click += new System.EventHandler(this.btnAlignC_Click);
			// 
			// btnAlignU
			// 
			this.btnAlignU.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignU.Location = new System.Drawing.Point(40, 16);
			this.btnAlignU.Name = "btnAlignU";
			this.btnAlignU.Size = new System.Drawing.Size(32, 32);
			this.btnAlignU.TabIndex = 6;
			this.btnAlignU.Text = "+";
			this.btnAlignU.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			this.btnAlignU.Click += new System.EventHandler(this.btnAlignU_Click);
			// 
			// btnAlignD
			// 
			this.btnAlignD.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignD.Location = new System.Drawing.Point(40, 80);
			this.btnAlignD.Name = "btnAlignD";
			this.btnAlignD.Size = new System.Drawing.Size(32, 32);
			this.btnAlignD.TabIndex = 8;
			this.btnAlignD.Text = "+";
			this.btnAlignD.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.btnAlignD.Click += new System.EventHandler(this.btnAlignD_Click);
			// 
			// btnAlignRU
			// 
			this.btnAlignRU.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignRU.Location = new System.Drawing.Point(72, 16);
			this.btnAlignRU.Name = "btnAlignRU";
			this.btnAlignRU.Size = new System.Drawing.Size(32, 32);
			this.btnAlignRU.TabIndex = 9;
			this.btnAlignRU.Text = "+";
			this.btnAlignRU.TextAlign = System.Drawing.ContentAlignment.TopRight;
			this.btnAlignRU.Click += new System.EventHandler(this.btnAlignRU_Click);
			// 
			// btnAlignRC
			// 
			this.btnAlignRC.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(186)));
			this.btnAlignRC.Location = new System.Drawing.Point(72, 48);
			this.btnAlignRC.Name = "btnAlignRC";
			this.btnAlignRC.Size = new System.Drawing.Size(32, 32);
			this.btnAlignRC.TabIndex = 10;
			this.btnAlignRC.Text = "+";
			this.btnAlignRC.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.btnAlignRC.Click += new System.EventHandler(this.btnAlignRC_Click);
			// 
			// btnDefault
			// 
			this.btnDefault.Location = new System.Drawing.Point(240, 24);
			this.btnDefault.Name = "btnDefault";
			this.btnDefault.TabIndex = 2;
			this.btnDefault.Text = "Default";
			this.btnDefault.Click += new System.EventHandler(this.btnDefault_Click);
			// 
			// gbMethod
			// 
			this.gbMethod.Controls.Add(this.rbMethodFitWidth);
			this.gbMethod.Controls.Add(this.rbMethodFitHeight);
			this.gbMethod.Controls.Add(this.rbMethodFit);
			this.gbMethod.Controls.Add(this.rbMethodKip);
			this.gbMethod.Location = new System.Drawing.Point(8, 16);
			this.gbMethod.Name = "gbMethod";
			this.gbMethod.Size = new System.Drawing.Size(104, 120);
			this.gbMethod.TabIndex = 0;
			this.gbMethod.TabStop = false;
			this.gbMethod.Text = "Method";
			// 
			// rbMethodFitWidth
			// 
			this.rbMethodFitWidth.Location = new System.Drawing.Point(16, 88);
			this.rbMethodFitWidth.Name = "rbMethodFitWidth";
			this.rbMethodFitWidth.Size = new System.Drawing.Size(80, 24);
			this.rbMethodFitWidth.TabIndex = 3;
			this.rbMethodFitWidth.Text = "fit to width";
			this.rbMethodFitWidth.CheckedChanged += new System.EventHandler(this.rbMethodFitWidth_CheckedChanged);
			// 
			// rbMethodFitHeight
			// 
			this.rbMethodFitHeight.Location = new System.Drawing.Point(16, 64);
			this.rbMethodFitHeight.Name = "rbMethodFitHeight";
			this.rbMethodFitHeight.Size = new System.Drawing.Size(80, 24);
			this.rbMethodFitHeight.TabIndex = 2;
			this.rbMethodFitHeight.Text = "fit to height";
			this.rbMethodFitHeight.CheckedChanged += new System.EventHandler(this.rbMethodFitHeight_CheckedChanged);
			// 
			// rbMethodFit
			// 
			this.rbMethodFit.Location = new System.Drawing.Point(16, 40);
			this.rbMethodFit.Name = "rbMethodFit";
			this.rbMethodFit.Size = new System.Drawing.Size(72, 24);
			this.rbMethodFit.TabIndex = 1;
			this.rbMethodFit.Text = "fit to size";
			this.rbMethodFit.CheckedChanged += new System.EventHandler(this.rbMethodFit_CheckedChanged);
			// 
			// rbMethodKip
			// 
			this.rbMethodKip.Checked = true;
			this.rbMethodKip.Location = new System.Drawing.Point(16, 16);
			this.rbMethodKip.Name = "rbMethodKip";
			this.rbMethodKip.Size = new System.Drawing.Size(64, 24);
			this.rbMethodKip.TabIndex = 0;
			this.rbMethodKip.TabStop = true;
			this.rbMethodKip.Text = "kip ratio";
			this.rbMethodKip.CheckedChanged += new System.EventHandler(this.rbMethodKip_CheckedChanged);
			// 
			// ResizeMethodControl
			// 
			this.Controls.Add(this.gbResizeMethod);
			this.Name = "ResizeMethodControl";
			this.Size = new System.Drawing.Size(344, 160);
			this.gbResizeMethod.ResumeLayout(false);
			this.gbAlign.ResumeLayout(false);
			this.gbMethod.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnDefault_Click(object sender, System.EventArgs e) {
			rbMethodKip.Checked = true;
			SetAlignButtonsDefaultColor();
			btnAlignC.BackColor = Color.Red;
			SetAlignEnabled(false);
		}

		private void SetAlignEnabled(bool enable){
			foreach(Control control in gbAlign.Controls){
				if (control is Button) {
					Button button = (Button)control;
					button.Enabled = enable;
				}
			}
		}

		private void rbMethodFitHeight_CheckedChanged(object sender, System.EventArgs e) {
			SetAlignEnabled(rbMethodFitHeight.Checked || rbMethodFitWidth.Checked);
		}

		private void rbMethodFitWidth_CheckedChanged(object sender, System.EventArgs e) {
			SetAlignEnabled(rbMethodFitHeight.Checked || rbMethodFitWidth.Checked);
		}

		private void rbMethodFit_CheckedChanged(object sender, System.EventArgs e) {
			SetAlignEnabled(rbMethodFitHeight.Checked || rbMethodFitWidth.Checked);
		}

		private void rbMethodKip_CheckedChanged(object sender, System.EventArgs e) {
			SetAlignEnabled(rbMethodFitHeight.Checked || rbMethodFitWidth.Checked);
		}

		private void SetAlignButtonsDefaultColor(){
			foreach(Button button in alignButtons) {
				button.BackColor = defaultColor;
			}
		}

		private void btnAlignLU_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignLU.BackColor = Color.Red;
			align = C.ALIGN_UP_LEFT;
		}

		private void btnAlignU_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignU.BackColor = Color.Red;
			align = C.ALIGN_UP;
		}

		private void btnAlignRU_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignRU.BackColor = Color.Red;
			align = C.ALIGN_UP_RIGHT;
		}

		private void btnAlignLC_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignLC.BackColor = Color.Red;
			align = C.ALIGN_CENTER_LEFT;
		}

		private void btnAlignRC_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignRC.BackColor = Color.Red;
			align = C.ALIGN_CENTER_RIGHT;
		}

		private void btnAlignLD_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignLD.BackColor = Color.Red;
			align = C.ALIGN_DOWN_LEFT;
		}

		private void btnAlignD_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignD.BackColor = Color.Red;
			align = C.ALIGN_DOWN;
		}

		private void btnAlignRD_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignRD.BackColor = Color.Red;
			align = C.ALIGN_DOWN_RIGHT;
		}

		private void btnAlignC_Click(object sender, System.EventArgs e) {
			SetAlignButtonsDefaultColor();
			btnAlignC.BackColor = Color.Red;	
			align = C.ALIGN_CENTER;
		}
	}
}
